//
//  TSUserInfoEditViewController.m
//  chart
//
//  Created by xu on 15/8/22.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSUserInfoEditViewController.h"
#import "TSToolBarTextField.h"

#define SEARCH_LABEL_WIDTH  50.f
#define SEARCH_LABEL_HEIGHT 35.f
#define SEARCH_ROW_GAP 10.f

@interface TSUserInfoEditViewController ()

@end

@implementation TSUserInfoEditViewController{
    TSToolBarTextField *titleTextField;
    TSToolBarTextField *workIdTextField;
    TSToolBarTextField *phoneTextField;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"更改个人信息"];
    
    [self addTitleRow];
    
    [self addWorkIdRow];
    
    [self addPhoneRow];
    
    WS(weakSelf);
    [self rightBarButtonWithTitle:@"修改" barNorImage:nil barHltImage:nil action:^{
        if (!weakSelf) {
            return;
        }
        
        [weakSelf editInfo];
    }];
}

- (void)editInfo
{
    if (![TSTools validateMobile:phoneTextField.text]) {
        [[LBToastManager manager] showtoast:@"请输入正确的手机号"];
        
        return;
    }
    
    WS(weakSelf);
    [[TSUserModel sharedInstance] modify:titleTextField.text andWorkId:workIdTextField.text andPhone:phoneTextField.text completionHandler:^(BOOL isSucceeded, NSError *error) {
        if (!weakSelf) {
            return;
        }
    
        if (isSucceeded) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:TSNotification_EditUserInfoSucceeded object:nil];
        }
    }];
}

- (UILabel *)createLabel:(CGRect)frame Title:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:title];
    [label setFont:FONT(12.f)];
    
    return label;
}

- (TSToolBarTextField *)createTextField:(CGRect)frame Title:(NSString *)title
{
    TSToolBarTextField *text_field = [[TSToolBarTextField alloc] initWithFrame:frame];
    [text_field setText:title];
    [text_field.layer setBorderWidth:.5f];
    [text_field.layer setCornerRadius:.5f];
    
    return text_field;
}

- (void)addTitleRow
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, MARGIN_TOP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"姓名:"];
    [self.view addSubview:label];
    
    titleTextField = [self createTextField:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), currentDeviceWidth-2*MARGIN_LEFT-SEARCH_LABEL_WIDTH-10.f, SEARCH_LABEL_HEIGHT) Title:[TSUserModel sharedInstance].userName];
    [self.view addSubview:titleTextField];
}

- (void)addWorkIdRow
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(titleTextField.frame)+SEARCH_ROW_GAP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"工号:"];
    [self.view addSubview:label];
    
    workIdTextField = [self createTextField:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), CGRectGetWidth(titleTextField.frame), SEARCH_LABEL_HEIGHT) Title:[TSUserModel sharedInstance].workId];
    [self.view addSubview:workIdTextField];
}

- (void)addPhoneRow
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(workIdTextField.frame)+SEARCH_ROW_GAP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"电话:"];
    [self.view addSubview:label];
    
    phoneTextField = [self createTextField:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), CGRectGetWidth(workIdTextField.frame), SEARCH_LABEL_HEIGHT) Title:[TSUserModel sharedInstance].phone];
    phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:phoneTextField];
}

@end
